

package top.continew.admin.question.user.service.questionset.entity;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import top.continew.admin.question.mapper.question.QuestionSetQuestionMapper;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionSet;
import top.continew.admin.question.model.question.QuestionSetQuestion;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;
import top.continew.admin.question.user.service.questionset.enums.QuestionSetTypeEnum;
import top.continew.admin.question.utils.SpringContextHolder;

import java.util.List;
import java.util.Objects;

public class WrongQuestionSet extends AbstractQuestionSet {

    private Long userId;

    public WrongQuestionSet(Long objectId) {
        QuestionSet questionSet = new QuestionSet().selectOne(Wrappers.lambdaQuery(QuestionSet.class)
            .or(item -> item.eq(QuestionSet::getUserId, objectId).or(item2 -> item2.eq(QuestionSet::getId, objectId)))
            .eq(QuestionSet::getType, QuestionSetTypeEnum.WRONG_QUESTION.getCode()));
        if (questionSet == null) {
            this.userId = objectId;
            this.createQuestionSet(new QuestionSet());
        } else {
            this.userId = questionSet.getUserId();
            this.questionSet = questionSet;
        }
    }

    public long getQuestionCount(Long specificationType, Integer status) {
        MPJLambdaWrapper<QuestionSetQuestion> wrapper = new MPJLambdaWrapper<QuestionSetQuestion>()
            .select("distinct t.question_id")
            .leftJoin(Question.class, Question::getId, QuestionSetQuestion::getQuestionId)
            .eq(Question::getOnlineStatus, 1L)
            .ne(Question::getType, QuestionTypeEnum.MATERIAL_QUESTIONS.getCode())
            .eq(QuestionSetQuestion::getStatus, status)
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId())
            .eq(Question::getSpecificationType, specificationType);
        return new QuestionSetQuestion().selectCount(wrapper);
    }

    @Override
    public void createQuestionSet(QuestionSet questionSet) {
        //获取随机一个题目
        questionSet.setType(QuestionSetTypeEnum.WRONG_QUESTION.getCode());
        questionSet.setUserId(userId);
        questionSet.setTitle("错题");
        super.createQuestionSet(questionSet);
    }

    public void markTheHighlights(Long questionId, Long isImportant) {
        List<QuestionSetQuestion> questionSetQuestion = new QuestionSetQuestion().selectList(Wrappers
            .lambdaQuery(QuestionSetQuestion.class)
            .eq(QuestionSetQuestion::getQuestionId, questionId)
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId()));
        for (QuestionSetQuestion question : questionSetQuestion) {
            question.setIsImportant(isImportant);
            question.updateById();
        }
    }

    @Override
    public void initQuestion(Long questionId) {
        Objects.requireNonNull(SpringContextHolder.getBean(QuestionSetQuestionMapper.class))
            .update(null, new LambdaUpdateWrapper<QuestionSetQuestion>(QuestionSetQuestion.class)
                .set(QuestionSetQuestion::getStatus, 0L)
                .eq(QuestionSetQuestion::getQuestionId, questionId)
                .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId()));
    }

}
